package cn.tiasen.helloworld;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.security.Permission;

public class CallSmsActivity extends AppCompatActivity {
    private Button btn_call;
    private Button btn_sms;
    private EditText et_call;
    private EditText et_sms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_sms);

        btn_call = findViewById(R.id.btn_call);
        btn_sms = findViewById(R.id.btn_sms);
        et_call = findViewById(R.id.et_call);
        et_sms = findViewById(R.id.et_sms);

        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = et_call.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(getApplicationContext(), "电话号码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + phone));
                    startActivity(intent);
                }
            }
        });
        btn_sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sms = et_sms.getText().toString();
                String phone = et_call.getText().toString();
                if (TextUtils.isEmpty(sms)) {
                    Toast.makeText(getApplicationContext(), "短信内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Intent.ACTION_SENDTO);
                    intent.setData(Uri.parse("smsto:" + phone));
                    intent.putExtra("sms_body", sms);
                    startActivity(intent);
                }
            }
        });

        btn_call.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String phone = et_call.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(getApplicationContext(), "电话号码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + phone));
                    startActivity(intent);
                }
                return true;
            }
        });
        btn_sms.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String sms = et_sms.getText().toString();
                String phone = et_call.getText().toString();
                if (TextUtils.isEmpty(sms) || TextUtils.isEmpty(phone)) {
                    Toast.makeText(getApplicationContext(), "短信内容或手机号不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phone,null,sms,null,null);
                    Toast.makeText(getApplicationContext(),"发送成功",Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
    }

}
